import * as sdk from 'baidu-aip-sdk';
import fs = require('fs');
import { LoggerUtility } from '../logger/logger.utility';

const AipSpeech = sdk.speech;
const AipNlpClient = sdk.nlp;
const appId = 'BGaXOmtTMmjeoLF4hVHmeBtZ';
const appKey = 'tUhzgd5GRZNo9Kf1bTHBnExNdGBgpfc1';
const client = new AipSpeech(0, appId, appKey);
const nlpAppId = '24822032';
const nlpApiKey = '9PuGOACEGCmNrTcVlO3fvnPR';
const nlpSecretKey = 'oiq0Bwahz9xUj4Oc9RP1i8HcIfFuViKp';
const nlpClient = new AipNlpClient(nlpAppId, nlpApiKey, nlpSecretKey);
export class Voice {
    public async RecognizeLocalFile(localUrl: string) {
        const voice = fs.readFileSync(localUrl);
        const voiceBase64 = Buffer.from(voice);
        // 识别本地语音文件
        return client.recognize(voiceBase64, 'pcm', 16000);
    }

    public async RecognizeOnline(url: string) {
        return client.recognizeByUrl(url, (data) => { LoggerUtility.info(data); }, 'amr', 8000);
    }

    public async text2Audio(text: string) {
        return client.text2audio(text, { spd: 0, per: 4 });
    }

    public async wordAnalysis(text: string) {
        return nlpClient.lexer(text);
    }

    public async wordAnalysisCustom(text: string) {
        return nlpClient.lexerCustom(text);
    }
}
